package com.mc.lp.database.service.handler;

import java.util.HashSet;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;

import com.mc.lp.database.exception.ConcurrentAccessException;
import com.mc.lp.database.service.handler.constant.FormRequestType;
import com.mc.lp.service.SportsService;
import com.mc.lp.shared.vo.IValueObject;
import com.mc.lp.shared.vo.SimpleResponse;
/*
 * @author aphanda
 * @date Oct 24, 2014
 * @time 10:27:03 PM
 */
import com.mc.lp.shared.vo.StudentRecordVO;

public class SportsRecordHandler extends AbstractFormHandler<IValueObject, IValueObject> {

	@Resource
	SportsService sportsService;

	@Override
	public HashSet<FormRequestType> getRequestHandlingType() {

		HashSet<FormRequestType> requestTypeHandled = new HashSet<FormRequestType>();
		requestTypeHandled.add(FormRequestType.SAVE_SPORTS);
		requestTypeHandled.add(FormRequestType.GET_SPORTS_BY_GRADE);
		return requestTypeHandled;
	}

	@Override
	public IValueObject serveRequest(FormRequestType requestType, IValueObject object)
			throws Exception {
		// simple response to be returned to UI calls if no match found,
		// otherwise return serviceResponseclass
		SimpleResponse response = new SimpleResponse();
		response.setId(1);
		response.setMsg("No case matched to perform operation, please check request type");

		// convert request to student record vo object
		StudentRecordVO studentVo = null;
		if (object != null)
			studentVo = (StudentRecordVO) object;

		switch (requestType) {
		case GET_SPORTS_BY_GRADE:
			IValueObject result = getSportsByGrade(studentVo);
			if (result == null) {
				response.setMsg("Not able to get data from server");
				return response;
			}
			return result;
		case SAVE_SPORTS:
			IValueObject saveResult = saveSports(studentVo);
			if (saveResult == null) {
				response.setMsg("Not able to save data from server");
				return response;
			}
			return saveResult;
		default:
			return response;
		}
	}

	/**
	 * get Sports record by grade id
	 * 
	 * @param object
	 * @return
	 */
	private IValueObject getSportsByGrade(StudentRecordVO object) {
		IValueObject result = null;
		if (object != null) {
			result = getSportsService().getSportsByEntityIdGrade(object);
		}
		return result;
	}

	/**
	 * save Sports
	 * 
	 * @param object
	 * @return
	 * @throws ConcurrentAccessException
	 * @throws DataAccessException
	 */
	private IValueObject saveSports(StudentRecordVO object) throws DataAccessException,
			ConcurrentAccessException {
		IValueObject result = null;
		if (object != null) {
			result = getSportsService().saveSports(object);
		}
		return result;
	}

	public SportsService getSportsService() {
		return sportsService;
	}

	public void setSportsService(SportsService sportsService) {
		this.sportsService = sportsService;
	}

}
